using System;
using System.Net;

namespace OnkyoController
{	
	public sealed class MainController : IDisposable
	{
        private readonly OnkyoIoHandler _ioHandler;
        private static MainController _onkyoControllerInstance;

	    public static MainController GetInstance()
        {
            if (_onkyoControllerInstance == null)
            {
                _onkyoControllerInstance = new MainController();
            }

            return _onkyoControllerInstance;
        }

        private MainController()
        {
            Connected = false;
            _ioHandler = new OnkyoIoHandler();
        }

	    public void WriteCommand(String command)
        {
            _ioHandler.WriteCommand(command);
        }

        public OnkyoControllerReturnCodes Connect(IPAddress ipAddress, int port)
        {
            var returnCode = _ioHandler.Connect(ipAddress, port);
            Connected = returnCode == OnkyoControllerReturnCodes.ConnectSuccess;
            return returnCode;
        }

	    public OnkyoControllerReturnCodes Disconnect()
	    {
            var returnCode = _ioHandler.Disconnect();
	        Connected = returnCode == OnkyoControllerReturnCodes.DisconnectFail;
	        return returnCode;
	    }

	    public void AddCommandListener(IOnkyoCommandListener listener)
	    {
	        _ioHandler.AddCommandListener(listener);
	    }

        public void ChangeVolume(double volume)
        {
            _ioHandler.ChangeVolume(volume);
        }

	    public void GetVolumeLevel()
	    {
	        _ioHandler.GetVolumeLevel();
	    }

	    public void GetArtist()
	    {
	        _ioHandler.GetArtist();
	    }

	    public void GetSongTitle()
	    {
	        _ioHandler.GetSongTitle();
	    }

        public void ChangeInput(String input)
        {
            _ioHandler.ChangeInput(input);
        }

	    public void NextTrack()
	    {
	        _ioHandler.NextTrack();
	    }

	    public void PreviousTrack()
	    {
	        _ioHandler.PreviousTrack();
	    }

	    public void Play()
	    {
	        _ioHandler.Play();
	    }

	    public void Pause()
	    {
	        _ioHandler.Pause();
	    }

	    public bool Connected { get; private set; }

	    public void Dispose()
        {
            if (_ioHandler != null)
            {
                _ioHandler.Dispose();
            }
        }
    }
}
